﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Iris.Engine;

namespace Iris.UnitTest
{
    class ImageTest
    {
        public string Nom;
        public int ScoreExcepted = 0;
        public bool IsLoaded = false;
    }

    class ScenarioRunner
    {


        List<ImageTest> ImagesTest = new List<ImageTest>() {
            new ImageTest() { Nom = "bleu.png", ScoreExcepted =3},
            new ImageTest() { Nom = "rouge.png", ScoreExcepted =3},
            new ImageTest() { Nom = "vert.png", ScoreExcepted =3},
            new ImageTest() { Nom = "jaune.png", ScoreExcepted =3},
        };

        List<ImageSort> data = new List<ImageSort>();

        ImageTest GetImageTest(string name)
        {
            return ImagesTest.Find(a => a.Nom == name);
        }


        public ScenarioRunner() {

            SortCore.singleton.LoadDataSourceEvent += LoadDataSource;
            SortCore.singleton.EndLoadDataSourceEvent += EndLoadDataSource;
            SortCore.singleton.LoadDuelEvent += LoadDuel;
            SortCore.singleton.LoadDuelPlusDeDuelEvent += LoadDuelPlusDeDuelEvent;
        }


        public void Run()
        {
            SortCore.singleton.BeginLoadDataSource(@".\test\");
        }


        void LoadDataSource(ImageSort image)
        {
            ImageTest imageTest = GetImageTest(image.DataSource.Name);

            if (imageTest == null)
            {
                UnitTest.Assert(false, "Chargement d'une image inatendu {0}", image.DataSource.Name);
            }
            else
            {
                imageTest.IsLoaded = true;
                UnitTest.Assert(true, "Chargement de l'image {0}", image.DataSource.Name);

                data.Add(image);
            }
        }

        void EndLoadDataSource()
        {
            UnitTest.Assert(true, "Chargement des images terminés");

            foreach (ImageTest imageTest in ImagesTest)
            {
                if (!imageTest.IsLoaded)
                {
                    UnitTest.Assert(false, "Chargement de l'image {0}", imageTest.Nom);
                }
            }

            SortCore.singleton.BeginLoadDuel();
        }

        void LoadDuel (ImageSort a, ImageSort b, int progression)
        {
            UnitTest.WriteLine("progression {0}, duel {1} vs {2}", progression, a.DataSource.Name, b.DataSource.Name);

            ImageTest testA = GetImageTest(a.DataSource.Name);
            ImageTest testB = GetImageTest(a.DataSource.Name);

            if (testA != null && testB != null)
            {

                SortCore.singleton.EndLoadDuelNull(a, b);
            }
            else {
                SortCore.singleton.EndLoadDuelNull(a, b);
            }

            SortCore.singleton.BeginLoadDuel();
        }

        void LoadDuelPlusDeDuelEvent()
        {
            data.ForEach(delegate(ImageSort a)
            {
                ImageTest testA = GetImageTest(a.DataSource.Name);
                UnitTest.Assert(testA.ScoreExcepted == a.Score, "{2} Score atendu {0} trouvé {1}", testA.ScoreExcepted, a.Score, testA.Nom);
            });

            //End
        }
    }
}
